﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using System.Globalization;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class NewCustomer : Form
    {
        TblCustomersDAO CustomersDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
        TblBookingDAO bookDAO = new TblBookingDAO(Global.CONNECTION_STRING);

        public delegate void SendMessage(string Message);
        public SendMessage Sender; 

        public NewCustomer()
        {
            InitializeComponent();
            //Tao con tro toi hàm GetMessage
            Sender = new SendMessage(GetMessage);
          
            LoadSuburb();
            addCombo_ContactMethod();
            addCombo_Title();
        }
        // add comboboc Contact Method
        private void addCombo_ContactMethod()
        {
            var method = new Dictionary<int, string>();
            method.Add(1, "Mobile");
            method.Add(2, "Email");
            method.Add(3, "HomePhone");
            method.Add(4, "WorkPhone");
            cbContactMethod.DataSource = new BindingSource(method, null);
            cbContactMethod.DisplayMember = "Value";
            cbContactMethod.ValueMember = "Key";
        }

        // add Combox Title
        private void addCombo_Title()
        {
            var title = new Dictionary<int, string>();
            title.Add(1, "Mr");
            title.Add(2, "Mss");
            cbTitle.DataSource = new BindingSource(title, null);
            cbTitle.DisplayMember = "Value";
            cbTitle.ValueMember = "Value";
        }

        //Hàm có nhiem vu lay tham so truyen vào
        private void GetMessage(string Message)
        {
            txtCusID.Text = Message;
        }

        private void NewCustomer_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
           
            if (txtCusID.Text == "")
            {
                btnNewBook.Enabled = false;
                btnNewPurchase.Enabled = false;
            }
            else
            {
                btnNewBook.Enabled = true;
                btnNewPurchase.Enabled = true;
            }
            txtFirstName.Focus();

            if (txtCusID.Text != "")
            {
                loadCustomerDetail();
                txtEmail.Enabled = false;
                txtPass.Enabled = false;
                loadJob();
                LoadCashPurchase();
            }
        }

        // load Suburb
        private void LoadSuburb()
        {
            cbSuburb.DataSource = CustomersDAO.GetSuburb_All();
            cbSuburb.DisplayMember = "suburb";
            cbSuburb.ValueMember = "suburb";
        }
        // load State
        private void LoadState()
        {
            cbState.DataSource = CustomersDAO.GetState_BySuburb(cbSuburb.SelectedValue.ToString(), txtPostCode.Text.Trim());
            cbState.DisplayMember = "state";
            cbState.ValueMember = "state";
        }
        private void cbSuburb_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = CustomersDAO.GetPostcode_ByState(cbSuburb.SelectedValue.ToString());
            foreach (DataRow dr in dt.Rows)
            {
                txtPostCode.Text = dr["postcode"].ToString();
                LoadState();
            }
        }

        private void loadCustomerDetail()
        {
            int id = Global.ConvertToInt(txtCusID.Text);
            TblCustomers customer = CustomersDAO.CustomerById(id);
            if (customer.CustomerFirstname == "")
            {
                txtFirstName.Text = customer.CustomerFullname;
            }
            else
            {
                txtFirstName.Text = customer.CustomerFirstname;
            }
            txtEmail.Text = customer.Emailaddress;
            txtCompany.Text = customer.Companyname;
            txtEmail.Text = customer.Emailaddress;
            cbTitle.Text = customer.CustomerTitle;
          
            txtLastName.Text = customer.CustomerLastname;
            txtMobile.Text = customer.Phonemobile;
            txtNumber.Text = customer.Postalnumber;
            // txtPass.Enabled = false;
            txtPostCode.Text = customer.Postalpostcode;
            txtStreet.Text = customer.Postalstreet;
            // txtUsername.Text = customerdt["CustomerUsername"].ToString();
            txtWorkPhone.Text = customer.Phonework;
            txtFax.Text = customer.Phonefacsimile;
            txtHomePhone.Text = customer.Phonehome;
            cbState.Text = customer.Postalstate;
            cbContactMethod.SelectedValue = customer.PreferContactMethod;

            btnNewBook.Enabled = true;
            btnNewPurchase.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                TblCustomers customer = new TblCustomers();
                customer.CustomerTitle = cbTitle.Text;
                customer.CustomerFirstname = txtFirstName.Text.Trim();
                customer.CustomerLastname = txtLastName.Text.Trim();
                customer.CustomerFullname = txtLastName.Text + " " + txtFirstName.Text;
                customer.Emailaddress = txtEmail.Text.Trim();
                customer.Emailpassword = Encryption.EncryptToString(txtPass.Text.Trim());
                customer.Companyname = txtCompany.Text.Trim();
                customer.DateCreated = DateTime.Now;
                customer.IsAdmin = false;
                customer.Phonehome = txtHomePhone.Text.Trim();
                customer.Phonemobile = txtMobile.Text.Trim();
                customer.Phonework = txtWorkPhone.Text.Trim();
                customer.Phonefacsimile = txtFax.Text.Trim();
                customer.Postalsuburb = cbSuburb.Text;
                customer.Postalpostcode = txtPostCode.Text;
                customer.Postalstate = cbState.Text;
                customer.Postalnumber = txtNumber.Text.Trim();
                customer.Postalstreet = txtStreet.Text.Trim();
             //   customer.PreferContactMethod = Global.ConvertToInt(cbContactMethod.ValueMember);
                customer.PreferContactMethod = Global.ConvertToInt(cbContactMethod.SelectedValue.ToString());

                DataTable dt = null;
                int id = Global.ConvertToInt(txtCusID.Text);

                if (txtCusID.Text == "") // insert new customer
                {
                    this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());

                    customer.CustomerIndex = 0;
                    dt = CustomersDAO.InsUpdCustomers(customer, 1);
                    foreach (DataRow dr in dt.Rows)
                    {
                        txtCusID.Text = dr["CustomerIndex"].ToString();
                    }
                }
                else
                {
                    txtEmail.Enabled = true;
                    txtPass.Enabled = true;
                    customer.CustomerIndex = id;
                    CustomersDAO.InsUpdCustomers(customer, 2);
                }

                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (flag)
            {
                MessageBox.Show("Save sucessful!");
                loadCustomerDetail();
            }
        }

        private void txtFirstName_TextChanged(object sender, EventArgs e)
        {
           // txtFirstName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtFirstName.Text);
         
        }

        private void txtLastName_TextChanged(object sender, EventArgs e)
        {
           // txtLastName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtLastName.Text);
        }

        private void txtNumber_TextChanged(object sender, EventArgs e)
        {
           // txtNumber.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtNumber.Text);
        }

        private void txtStreet_TextChanged(object sender, EventArgs e)
        {
           // txtStreet.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtStreet.Text);
        }

        private void btnNewCus_Click(object sender, EventArgs e)
        {
            btnNewCus.Enabled = false;
            txtCompany.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtEmail.Enabled = true;
            txtFax.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtHomePhone.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtMobile.Text = string.Empty;
            txtNumber.Text = string.Empty;
            txtPass.Text = string.Empty;
            txtPass.Enabled = true;
            txtPostCode.Text = string.Empty;
            txtStreet.Text = string.Empty;
            txtWorkPhone.Text = string.Empty;
            btnSave.Visible = true;
        }

        private void loadJob()
        {     
            try
            {
                int id = Global.ConvertToInt(txtCusID.Text);
                TblJobsDAO jobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
                DataTable dt = jobDAO.GetJobByCustomerID(id);
                grdJob.DataSource = dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //load purchase
        private void LoadCashPurchase()
        {
            // noi dung booking
            DataTable dt = bookDAO.GetBookingByCus(Global.ConvertToInt(txtCusID.Text));
            grdPurchase.DataSource = dt;
        }

        private void btnNewBook_Click(object sender, EventArgs e)
        {
            if (txtCusID.Text != "")
            {
                NewBooking new_booking = new NewBooking();
                new_booking.Sender(txtCusID.Text);
                this.Hide();
                new_booking.Show();
            }
        }

        private void btnNewPurchase_Click(object sender, EventArgs e)
        {
            string idCus = txtCusID.Text;
            Purchase purchase = new Purchase();
            purchase.Sender(idCus); //Gọi delegate
            this.Hide();
            purchase.Show();  
        }
    }
}
